<script type="text/html" template lay-done="layui.data.sendParams(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-CoreCmsOrderEdit-editForm" id="LAY-app-CoreCmsOrderEdit-editForm">
        <input type="hidden" name="id" id="id" value="{{d.params.data.model.id || '' }}" />

        <!--<blockquote class="layui-elem-quote">
        目前本系统暂无密码加账号功能，但是预留了相关字段，可自行对接。
    </blockquote>-->
        <!--<div class="layui-form-item">
        <div class="layui-inline  layui-input-wrap layui-input-wrap-prefix">
            <label class="layui-form-label layui-form-required">来源：</label>
            <div class="layui-input-inline">
                <select name="sourceId" lay-verify="required">
                    <option value="" selected="selected">请选择</option>
                    {{# layui.each(d.params.data.sourceList, function(index, item){ }}
                    <option value="{{ item.id }}" {{ item.id == d.params.data.model.sourceId ? 'selected="selected"' : '' }}>{{ item.userName }}</option>
                    {{# }); }}
                </select>
            </div>
        </div>
        <div class="layui-inline  layui-input-wrap layui-input-wrap-prefix">
            <label class="layui-form-label layui-form-required">客户分类：</label>
            <div class="layui-input-inline">

                <select name="customerClassId">
                    <option value="" selected="selected">请选择</option>
                    {{# layui.each(d.params.data.classList, function(index, item){ }}
                    <option value="{{ item.id }}" {{ item.id == d.params.data.model.customerClassId ? 'selected="selected"' : '' }}>{{ item.className }}</option>
                    {{# }); }}
                </select>
            </div>
        </div>
    </div>-->
        <div class="layui-form-item">
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">客户名字：</label>
                <div class="layui-input-inline">
                    <input type="text" name="shipName" required lay-verify="required" class="layui-input" lay-affix="clear" value="{{d.params.data.model.shipName || '' }}">
                </div>
            </div>
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">手机号：</label>
                <div class="layui-input-inline">
                    <input type="text" name="shipMobile" required lay-verify="required|phone" class="layui-input" lay-affix="clear" value="{{d.params.data.model.shipMobile || '' }}">
                </div>
            </div>
        </div>
        <!--<div class="layui-form-item layui-input-wrap layui-input-wrap-prefix">
        <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
            <label class="layui-form-label">备用手机：</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" class="layui-input" lay-affix="clear" value="{{d.params.data.model.phone || '' }}">
            </div>
        </div>
        <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
            <label class="layui-form-label">客户微信号：</label>
            <div class="layui-input-inline">
                <input type="text" name="customerWx" class="layui-input" lay-affix="clear" value="{{d.params.data.model.customerWx || '' }}">
            </div>
        </div>
    </div>-->
        <!--<div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">性别：</label>
            <div class="layui-input-inline">

                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-username"></i>
                </div>
                <select name="sex" required>
                    <option value="1" {{d.params.data.model.sex ===1 ? 'selected="selected"':'' }}>男</option>
                    <option value="2" {{d.params.data.model.sex ===2 ? 'selected="selected"':'' }}>女</option>
                    <option value="3" {{d.params.data.model.sex ===3 ? 'selected="selected"':'' }}>未知</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">加粉日期：</label>
            <div class="layui-input-inline">
                <input type="text" name="birthday" id="editTime-birthday" class="layui-input" value="{{ d.params.data.model.birthday ? layui.util.toDateString(d.params.data.model.birthday, 'yyyy-MM-dd') : '' }}">
            </div>
        </div>
    </div>-->
        <div class="layui-form-item">
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">客户编号：</label>
                <div class="layui-input-inline">
                    <input type="text" id="number" name="number" class="layui-input" readonly="readonly" value="{{d.params.data.model.user.number || '' }}">
                </div>
            </div>
            <div class="layui-inline  layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">归属销售：</label>
                <div class="layui-input-inline">
                    <select name="sysid" lay-verify="required" id="sysid">
                        <option value="" selected="selected">请选择</option>
                        {{# layui.each(d.params.data.uuserList, function(index, item){ }}
                        <option value="{{ item.id }}" {{ item.id == d.params.data.model.sysid ? 'selected="selected"' : '' }}>{{ item.nickName }}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">订单总金额：</label>
                <div class="layui-input-inline">
                    <input type="text" id="orderAmount" name="orderAmount" lay-verify="required|number" required class="layui-input" lay-affix="clear" placeholder="订单总金额" value="{{d.params.data.model.orderAmount || '' }}">
                </div>
            </div>
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">代收总金额：</label>
                <div class="layui-input-inline">
                    <input type="text" id="goodsAmount" name="goodsAmount" lay-verify="required|number" required class="layui-input" lay-affix="clear" placeholder="订单总金额" value="{{d.params.data.model.goodsAmount || '' }}">
                </div>
            </div>
            <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">定金总金额：</label>
                <div class="layui-input-inline">
                    <input type="text" id="payedAmount" name="payedAmount" lay-verify="required|number" required class="layui-input" lay-affix="clear" placeholder="订单总金额" value="{{d.params.data.model.payedAmount || '' }}">
                </div>
            </div>
            <div class="layui-inline  layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">支付方式：</label>
                <div class="layui-input-inline">
                    <select name="paymentCode" lay-verify="required" id="paymentCode">
                        <option value="" selected="selected">请选择</option>
                        {{# layui.each(d.params.data.newPaymentsList, function(index, item){ }}
                        <option value="{{ item.description }}" {{ item.description == d.params.data.model.paymentCode ? 'selected="selected"' : '' }}>
                            {{ item.description }}
                        </option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline  layui-input-wrap layui-input-wrap-prefix">
                <label class="layui-form-label layui-form-required">订单类型：</label>
                <div class="layui-input-inline">
                    <select name="orderTypeName" lay-verify="required" id="orderTypeName">
                        <option value="" selected="selected">订单类型</option>
                        {{# layui.each(d.params.data.orderTypeCRMList, function(index, item){ }}
                        <option value="{{ item.description }}" {{ item.description == d.params.data.model.orderTypeName ? 'selected="selected"' : '' }}>
                            {{ item.description }}
                        </option>
                        {{# }); }}
                    </select>
                </div>
                <div class="layui-inline layui-input-wrap layui-input-wrap-prefix">
                    <label class="layui-form-label layui-form-required">订单号：</label>
                    <div class="layui-input-inline">
                        <input type="text" id="orderId" name="orderId" class="layui-input" readonly="readonly" value="{{d.params.data.model.orderId || '' }}">
                    </div>
                </div>
            </div>
        </div>
        <!--<div class="layui-form-item">
        <label for="name" class="layui-form-label">客户编号</label>
        <div class="layui-input-block">
            <input name="number" id="number" class="layui-input" value="{{d.params.data.model.number || '' }}" readonly="readonly" />
        </div>

    </div>-->
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">收货地址</label>
            <div class="layui-input-block">
                <input name="address" id="address" class="layui-input" value="{{d.params.data.model.shipAddress || '' }}" />
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单备注</label>
            <div class="layui-input-block">
                <input type="text" name="mark" class="layui-input" value="{{d.params.data.model.mark || '' }}">
            </div>
        </div>
        <div class="layui-form-item">
            <form class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">选择地区</label>
                    <div class="layui-input-inline">
                        <select name="province" lay-filter="province">
                            <option value="">请选择省</option>
                        </select>
                    </div>
                    <div class="layui-input-inline" style="display: none;">
                        <select name="city" lay-filter="city">
                            <option value="">请选择市</option>
                        </select>
                    </div>
                    <div class="layui-input-inline" style="display: none;">
                        <select name="area" lay-filter="area">
                            <option value="">请选择县/区</option>
                        </select>
                    </div>
                </div>
                <label for="name" class="layui-form-label">新地址</label>
                <div class="layui-input-block">
                    <input name="newshipAddress" id="newshipAddress" class="layui-input" lay-reqText="请输入地址" placeholder="请输入地址" />
                </div>
                <!--<input id="address" type="text" name="addressc" class="layui-input" lay-affix="clear">>newshipAddress-->
            </form>

            <!--<label for="name" class="layui-form-label layui-form-required">地址</label>
        <div class="layui-input-block">
            <input name="address" id="address"  class="layui-input" lay-reqText="请输入地址" placeholder="请输入地址" />
        </div>>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <!--<input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-CoreCmsOrderEditNewInfo-editForm-submit" id="LAY-app-CoreCmsOrderEditNewInfo-editForm-submit" value="更新资料">-->
            <button class="layui-btn" lay-active="goodsShow">更新产品</button>
            <!--<input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-CoreCmsUser-saveAddOrderForm-submit" id="LAY-app-CoreCmsUser-saveAddOrderForm-submit" value="提交订单">-->
            <!--<button class="layui-btn layui-btn-sm" lay-active="saveOrder">保存订单</button>-->
            <input type="hidden" name="goodsId" id="goodsId" value="" class="layui-input">
            <input type="hidden" name="productsCount" id="productsCount" value="0">
            <!--<input type="button" class="layui-btn" lay-active="goods_show" value="添加产品">-->
        </div>
        <div class="layui-form-item text-right core-hidden">
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-CoreCmsOrder-editForm-submit" id="LAY-app-CoreCmsOrder-editForm-submit" value="确认编辑">
        </div>
        <div class="table-body" id="total-box">
            <table id="LAY-app-CoreCmsNewCart-tableBox" lay-filter="LAY-app-CoreCmsNewCart-tableBox"></table>
        </div>
        <!--<div class="layui-form-item">
        <div>

        </div>

    </div>-->
        <!--<div class="layui-tab layui-tab-card">
        <ul class="layui-tab-title">
            <li class="layui-this">订单记录</li>
            <li>跟进记录</li>
            <li>操作记录</li>
            <li>物流信息</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">


                <table id="LAY-app-CoreCmsOrderRecords-tableBox" class="layui-tab-item layui-show" lay-filter="LAY-app-CoreCmsOrderRecords-tableBox"></table>

            </div>
            <div class="layui-tab-item">

                <table id="LAY-app-CoreCmsFollowRecordsInfo-tableBox" lay-filter="LAY-app-CoreCmsFollowRecordsInfo-tableBox"></table>

            </div>

            <div class="layui-tab-item">
                <table id="LAY-app-CoreCmsOrderLogRecords-tableBox" lay-filter="LAY-app-CoreCmsOrderLogRecords-tableBox"></table>
            </div>
            <div class="layui-tab-item">内容-5</div>
        </div>
    </div>-->



        <div id="LAY-popup-CoreCmsUser-EditPoint"></div>
    </div>

</script>




<script type="text/html" id="LAY-app-CoreCmsNewCart-tableBox-bar">
    <!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../../../lib/jquery/address.js"></script>
<script>var areaData = Area;</script>
<script>
    var ids = {};

    layui.data.sendParams = function (d) {
        console.log(d);

        layui.use(['admin', 'form', 'laydate', 'upload', 'coreHelper', 'cropperImg', 'view', 'util', 'laytpl', 'table'],

            function () {
                var $ = layui.$
                    , form = layui.form
                    , admin = layui.admin
                    , laydate = layui.laydate
                    , upload = layui.upload
                    , cropperImg = layui.cropperImg
                    , coreHelper = layui.coreHelper
                    , util = layui.util
                    , laytpl = layui.laytpl
                    , table = layui.table,
                    view = layui.view;

                var selectedProvince = '';
                var selectedCity = '';
                var selectedArea = '';

                $form = $('form');
                loadProvince();
                // 加载省数据
                function loadProvince() {
                    var proHtml = '<option value="">请选择省</option>';
                    for (var i = 0; i < areaData.length; i++) {
                        proHtml += '<option value="' + areaData[i].provinceCode + '_' + areaData[i].mallCityList.length + '_' + i + '">' + areaData[i].provinceName + '</option>';
                    }
                    $form.find('select[name=province]').html(proHtml);
                    form.render();

                    form.on('select(province)', function (data) {
                        selectedProvince = data.elem.options[data.elem.selectedIndex].text;
                        selectedCity = ''; // 重置市
                        selectedArea = ''; // 重置区

                        $form.find('select[name=area]').html('<option value="">请选择县/区</option>').parent().hide();
                        var value = data.value;
                        var d = value.split('_');
                        var count = d[1];
                        var index = d[2];

                        if (count > 0) {
                            loadCity(areaData[index].mallCityList);
                        } else {
                            $form.find('select[name=city]').parent().hide();
                        }
                        updateAddress();
                    });
                }

       

                // 加载市数据
                function loadCity(citys) {
                    var cityHtml = '<option value="">请选择市</option>';
                    for (var i = 0; i < citys.length; i++) {
                        cityHtml += '<option value="' + citys[i].cityCode + '_' + citys[i].mallAreaList.length + '_' + i + '">' + citys[i].cityName + '</option>';
                    }
                    $form.find('select[name=city]').html(cityHtml).parent().show();
                    form.render();

                    form.on('select(city)', function (data) {
                        selectedCity = data.elem.options[data.elem.selectedIndex].text;
                        selectedArea = ''; // 重置区
                        
                        var value = data.value;
                        var d = value.split('_');
                        var count = d[1];
                        var index = d[2];

                        if (count > 0) {
                            loadArea(citys[index].mallAreaList);
                        } else {
                            $form.find('select[name=area]').parent().hide();
                        }
                        updateAddress();
                    });
                }
                console.log(d.params.data.model.number ,'sdfa---->')
                //数据绑定跟进记录
  
                // 加载区数据
                function loadArea(areas) {
                    var areaHtml = '<option value="">请选择县/区</option>';
                    for (var i = 0; i < areas.length; i++) {
                        areaHtml += '<option value="' + areas[i].areaCode + '">' + areas[i].areaName + '</option>';
                    }
                    $form.find('select[name=area]').html(areaHtml).parent().show();
                    form.render();

                    form.on('select(area)', function (data) {
                        selectedArea = data.elem.options[data.elem.selectedIndex].text;
                        updateAddress();
                    });
                }

                // 更新地址输入框
                function updateAddress() {
                    $('#newshipAddress').val(
                        (selectedProvince ? selectedProvince + ' ' : '') +
                        (selectedCity ? selectedCity + ' ' : '') +
                        (selectedArea ? selectedArea : '')
                    );
                }

                laydate.render({
                    elem: '#editTime-birthday',
                });

                laydate.render({
                    elem: '#editTime-birthday',
                });
                //监听工具条
                table.on('tool(LAY-app-CoreCmsNewCart-tableBox)',
                    function (obj) {
                        if (obj.event === 'detail') {
                            doDetails(obj);
                        } else if (obj.event === 'del') {
                            doDelete(obj);
                        } else if (obj.event === 'edit') {
                            doEdit(obj)
                        }
                    });
                //执行单个删除
                function doDelete(obj) {
                    layer.confirm('确定删除吗？删除后将无法恢复。', function (index) {
                        coreHelper.Post("Api/CoreCmsOrder/DoDeleteProduct", { id: obj.data.id }, function (e) {
                            table.reloadData('LAY-app-CoreCmsNewCart-tableBox');
                            layer.msg(e.msg);
                        });
                    });
                }
                //数据绑定
                table.render({
                    elem: '#LAY-app-CoreCmsNewCart-tableBox',
                    url: layui.setter.apiUrl + 'Api/CoreCmsOrder/GetCoreCmsOrderItem',
                    method: 'POST',
                   // toolbar: '#LAY-app-CoreCmsNewCart-toolbar',
                    pagebar: '#LAY-app-CoreCmsNewCart-pagebar',
                    className: 'pagebarbox',
                    defaultToolbar: [],
                    height: '',//面包屑142px,搜索框4行172,3行137,2行102,1行67
                    page: true,
                    limit: 30,
                    where: { orderId: d.params.data.model.orderId },
                    limits: [10, 15, 20, 25, 30, 50, 100, 200],
                    text: { none: '暂无相关数据' },
                    cols: [
                        [
                            { field: 'name', title: '产品名字', sort: false, width: 500 }
                            , { field: 'price', title: '价格', sort: false, width: 105 }
                            , { field: 'nums', title: '可改数量', sort: false, width: 105, edit: true },
                            { width: 162, align: 'center', title: '操作', fixed: 'right', toolbar: '#LAY-app-CoreCmsNewCart-tableBox-bar' }
                        ]
                    ],

                    done: function (res, curr, count) {
                        // 获取分页容器

                        console.log('qqweqew-防守打法-->')
                        const total = res.total //怎么把你接口的数据赋值到这里
                        console.log(res, 'res--->')
                        let pagebar = document.querySelector('#total-box .layui-table-page');
                        console.log(pagebar, 'pagebar----->')
                        if (res.data.length) {
                            $('#orderAmount').show()
                            $('#total-box').show()
                        } else {
                            $('#orderAmount').hide()
                            $('#total-box').hide()
                        }
                        
                        if (pagebar) {
                            let totalDiv = document.querySelector('#totalAmountDisplay');
                            if (!totalDiv) {
                                totalDiv = document.createElement('div');
                                totalDiv.id = 'totalAmountDisplay';
                                totalDiv.style.cssText = 'display:inline-block;margin-right:20px;font-size:14px;color:#333;';
                                pagebar.insertBefore(totalDiv, pagebar.firstChild);
                            }
                           /* totalDiv.innerHTML = `产品总金额：¥ ${total}`;*/
                        }
                    }
                });

                // 单元格编辑后的事件
                table.on('edit(LAY-app-CoreCmsNewCart-tableBox)', function (obj) {
                  
                    console.log(obj.data, 'ob-->')
                    console.log(obj.data.nums, 'o-----dd--->')
                    var count = obj.data.nums;
                    var field = obj.data; //获取提交的字段
                    //obj.data.objectId = 1;
                    //obj.data.type = 1;
                    // 值的校验
                    if (count.replace(/\s/g, '') === '') {
                        obj.data.nums = 1;
                        return obj.reedit(); // 重新编辑 -- v2.8.0 新增
                    }
                    if (count == 0) {
                        obj.data.nums = 1;
                        return obj.reedit();
                    }
                    layui.table.reloadData('LAY-app-CoreCmsNewCart-tableBox'); //重载表格
                    coreHelper.Post("Api/CoreCmsOrder/DoEditNums", { id: obj.data.id, nums: obj.data.nums, orderId: d.params.data.model.orderId }, function (e) {
                                
                            if (e.code === 0) {
                                layui.table.reloadData('LAY-app-CoreCmsNewCart-tableBox'); //重载表格
                                layui.table.reloadData('LAY-app-CoreCmsOrder-tableBox'); //重载表格
                                //layer.close(index); //再执行关闭
                               // layer.msg(e.msg);
                            } else {
                                layer.msg(e.msg);
                            }
                     
                    });
                    // 编辑后续操作
                    /* layer.msg('数量的值更改值为：' + util.escape(value));*/
                    
                    form.render();

                });

                //处理属性 为 lay-active 的所有元素事件
                util.event('lay-active', {
                    goodsShow: function () {
                        admin.popup({
                            shadeClose: false,
                            title: '选择商品',
                            area: ['900px', '80%'],
                            id: 'LAY-app-CoreCmsPinTuanRule-createForm-GetGoodsList',
                            success: function (layero, index) {
                                view(this.id).render('common/getProductsIdsForOrder', null).done(function () {

                                    form.render();
                                    form.on('submit(LAY-app-CoreCmsProducts-getData)',
                                        function (data) {
                                            console.log(ids, '--1--data-213123----->')
                                            var the_val = "";
                                            var field = ids; //获取提交的字段
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                            coreHelper.Post("Api/CoreCmsOrder/AddOrderItem", Object.values(field).map(item => {
                                                // item.a = '12313' 就这样写就好了
                                                item.userId = d.params.data.model.id
                                                item.number = d.params.data.model.user.number
                                                item.orderId = d.params.data.model.orderId
                                                return item
                                            }), function (e) {
                                                if (e.code==0) {
                                                    layui.table.reloadData('LAY-app-CoreCmsNewCart-tableBox'); //重载表格
                                                    layer.msg(e.msg);
                                                }
                                                    console.log("回来啦",e)
                                             
                                                });
                                         
                                                    form.render();
                                                    layer.close(index);
                                        });

                                });
                            }
                        });
                    },
                });




                form.verify({
                    verifyuserName: [/^[\S]{0,20}$/, '用户名最大只允许输入20位字符，且不能出现空格'],
                    verifypassWord: [/^[\S]{0,50}$/, '密码最大只允许输入50位字符，且不能出现空格'],
                    verifymobile: [/^[\S]{0,15}$/, '手机号最大只允许输入15位字符，且不能出现空格'],
                    verifyavatarImage: [/^[\S]{0,255}$/, '头像最大只允许输入255位字符，且不能出现空格'],
                    verifynickName: [/^[\S]{0,50}$/, '昵称最大只允许输入50位字符，且不能出现空格'],
                });
                //重载form
                form.render(null, 'LAY-app-CoreCmsUser-editForm');
            })
    };
</script>